package org.apache.commons.httpclient;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import org.apache.commons.httpclient.util.EncodingUtil;
import org.apache.commons.httpclient.util.ExceptionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpConnection
{
  private static final byte[] CRLF;
  private static final Log LOG;
  static Class class$org$apache$commons$httpclient$HttpConnection;
  private String hostName;
  private HttpConnectionManager httpConnectionManager;
  private InputStream inputStream;
  protected boolean isOpen;
  private InputStream lastResponseInputStream;
  private InetAddress localAddress;
  private boolean locked;
  private OutputStream outputStream;
  private HttpConnectionParams params;
  private int portNumber;
  private Protocol protocolInUse;
  private String proxyHostName;
  private int proxyPortNumber;
  private Socket socket;
  private boolean tunnelEstablished;
  private boolean usingSecureSocket;

  static
  {
    byte[] arrayOfByte = new byte[2];
    arrayOfByte[0] = 13;
    arrayOfByte[1] = 10;
    CRLF = arrayOfByte;
    Class localClass;
    if (class$org$apache$commons$httpclient$HttpConnection == null)
    {
      localClass = class$("org.apache.commons.httpclient.HttpConnection");
      class$org$apache$commons$httpclient$HttpConnection = localClass;
    }
    while (true)
    {
      LOG = LogFactory.getLog(localClass);
      return;
      localClass = class$org$apache$commons$httpclient$HttpConnection;
    }
  }

  public HttpConnection(String paramString, int paramInt)
  {
    this(null, -1, paramString, null, paramInt, Protocol.getProtocol("http"));
  }

  public HttpConnection(String paramString1, int paramInt1, String paramString2, int paramInt2)
  {
    this(paramString1, paramInt1, paramString2, null, paramInt2, Protocol.getProtocol("http"));
  }

  public HttpConnection(String paramString1, int paramInt1, String paramString2, int paramInt2, Protocol paramProtocol)
  {
    this.hostName = null;
    this.portNumber = -1;
    this.proxyHostName = null;
    this.proxyPortNumber = -1;
    this.socket = null;
    this.inputStream = null;
    this.outputStream = null;
    this.lastResponseInputStream = null;
    this.isOpen = false;
    this.params = new HttpConnectionParams();
    this.locked = false;
    this.usingSecureSocket = false;
    this.tunnelEstablished = false;
    if (paramString2 == null)
      throw new IllegalArgumentException("host parameter is null");
    if (paramProtocol == null)
      throw new IllegalArgumentException("protocol is null");
    this.proxyHostName = paramString1;
    this.proxyPortNumber = paramInt1;
    this.hostName = paramString2;
    this.portNumber = paramProtocol.resolvePort(paramInt2);
    this.protocolInUse = paramProtocol;
  }

  public HttpConnection(String paramString1, int paramInt1, String paramString2, String paramString3, int paramInt2, Protocol paramProtocol)
  {
    this(paramString1, paramInt1, paramString2, paramInt2, paramProtocol);
  }

  public HttpConnection(String paramString, int paramInt, Protocol paramProtocol)
  {
    this(null, -1, paramString, null, paramInt, paramProtocol);
  }

  public HttpConnection(String paramString1, String paramString2, int paramInt, Protocol paramProtocol)
  {
    this(null, -1, paramString1, paramString2, paramInt, paramProtocol);
  }

  public HttpConnection(HostConfiguration paramHostConfiguration)
  {
    this(paramHostConfiguration.getProxyHost(), paramHostConfiguration.getProxyPort(), paramHostConfiguration.getHost(), paramHostConfiguration.getPort(), paramHostConfiguration.getProtocol());
    this.localAddress = paramHostConfiguration.getLocalAddress();
  }

  static Class class$(String paramString)
  {
    try
    {
      Class localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }

  protected void assertNotOpen()
    throws IllegalStateException
  {
    if (!(this.isOpen))
      return;
    throw new IllegalStateException("Connection is open");
  }

  protected void assertOpen()
    throws IllegalStateException
  {
    if (this.isOpen)
      return;
    throw new IllegalStateException("Connection is not open");
  }

  public void close()
  {
    LOG.trace("enter HttpConnection.close()");
    closeSocketAndStreams();
  }

  public boolean closeIfStale()
    throws IOException
  {
    if ((this.isOpen) && (isStale()))
    {
      LOG.debug("Connection is stale, closing...");
      close();
    }
    for (int i = 1; ; i = 0)
      return i;
  }

  protected void closeSocketAndStreams()
  {
    LOG.trace("enter HttpConnection.closeSockedAndStreams()");
    this.isOpen = false;
    this.lastResponseInputStream = null;
    OutputStream localOutputStream;
    if (this.outputStream != null)
    {
      localOutputStream = this.outputStream;
      this.outputStream = null;
    }
    label43: InputStream localInputStream;
    try
    {
      localOutputStream.close();
      if (this.inputStream != null)
      {
        localInputStream = this.inputStream;
        label85: this.inputStream = null;
      }
    }
    catch (Exception localException2)
    {
      Socket localSocket;
      try
      {
        localInputStream.close();
        if (this.socket != null)
        {
          localSocket = this.socket;
          this.socket = null;
        }
      }
      catch (Exception localException2)
      {
        try
        {
          localSocket.close();
          this.tunnelEstablished = false;
          this.usingSecureSocket = false;
          return;
          localException3 = localException3;
          LOG.debug("Exception caught when closing output", localException3);
          break label43:
          localException2 = localException2;
          LOG.debug("Exception caught when closing input", localException2);
        }
        catch (Exception localException1)
        {
          LOG.debug("Exception caught when closing socket", localException1);
          break label85:
        }
      }
    }
  }

  public void flushRequestOutputStream()
    throws IOException
  {
    LOG.trace("enter HttpConnection.flushRequestOutputStream()");
    assertOpen();
    this.outputStream.flush();
  }

  public String getHost()
  {
    return this.hostName;
  }

  public HttpConnectionManager getHttpConnectionManager()
  {
    return this.httpConnectionManager;
  }

  public InputStream getLastResponseInputStream()
  {
    return this.lastResponseInputStream;
  }

  public InetAddress getLocalAddress()
  {
    return this.localAddress;
  }

  public HttpConnectionParams getParams()
  {
    return this.params;
  }

  public int getPort()
  {
    if (this.portNumber < 0)
      if (!(isSecure()));
    for (int i = 443; ; i = this.portNumber)
      while (true)
      {
        return i;
        i = 80;
      }
  }

  public Protocol getProtocol()
  {
    return this.protocolInUse;
  }

  public String getProxyHost()
  {
    return this.proxyHostName;
  }

  public int getProxyPort()
  {
    return this.proxyPortNumber;
  }

  public OutputStream getRequestOutputStream()
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.getRequestOutputStream()");
    assertOpen();
    Object localObject = this.outputStream;
    if (Wire.CONTENT_WIRE.enabled())
      localObject = new WireLogOutputStream((OutputStream)localObject, Wire.CONTENT_WIRE);
    return ((OutputStream)localObject);
  }

  public InputStream getResponseInputStream()
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.getResponseInputStream()");
    assertOpen();
    return this.inputStream;
  }

  public int getSendBufferSize()
    throws SocketException
  {
    if (this.socket == null);
    for (int i = -1; ; i = this.socket.getSendBufferSize())
      return i;
  }

  public int getSoTimeout()
    throws SocketException
  {
    return this.params.getSoTimeout();
  }

  protected Socket getSocket()
  {
    return this.socket;
  }

  public String getVirtualHost()
  {
    return this.hostName;
  }

  protected boolean isLocked()
  {
    return this.locked;
  }

  public boolean isOpen()
  {
    return this.isOpen;
  }

  public boolean isProxied()
  {
    if ((this.proxyHostName != null) && (this.proxyPortNumber > 0));
    for (int i = 1; ; i = 0)
      return i;
  }

  public boolean isResponseAvailable()
    throws IOException
  {
    LOG.trace("enter HttpConnection.isResponseAvailable()");
    if (this.isOpen)
      if (this.inputStream.available() <= 0);
    for (int i = 1; ; i = 0)
      while (true)
      {
        return i;
        i = 0;
      }
  }

  public boolean isResponseAvailable(int paramInt)
    throws IOException
  {
    LOG.trace("enter HttpConnection.isResponseAvailable(int)");
    assertOpen();
    int i = 0;
    if (this.inputStream.available() > 0)
    {
      i = 1;
      label29: return i;
    }
    while (true)
      try
      {
        while (true)
        {
          this.socket.setSoTimeout(paramInt);
          this.inputStream.mark(1);
          if (this.inputStream.read() == -1)
            break label116;
          this.inputStream.reset();
          LOG.debug("Input data available");
          i = 1;
          try
          {
            this.socket.setSoTimeout(this.params.getSoTimeout());
          }
          catch (IOException localIOException3)
          {
            LOG.debug("An error ocurred while resetting soTimeout, we will assume that no response is available.", localIOException3);
            i = 0;
          }
        }
        label158: label116: break label29:
      }
      catch (InterruptedIOException localInterruptedIOException)
      {
        throw localInterruptedIOException;
      }
      finally
      {
        try
        {
          while (true)
          {
            this.socket.setSoTimeout(this.params.getSoTimeout());
            throw localObject;
            if (LOG.isDebugEnabled())
              LOG.debug("Input data not available after " + paramInt + " ms");
            try
            {
              this.socket.setSoTimeout(this.params.getSoTimeout());
            }
            catch (IOException localIOException2)
            {
              LOG.debug("An error ocurred while resetting soTimeout, we will assume that no response is available.", localIOException2);
              i = 0;
            }
          }
        }
        catch (IOException localIOException1)
        {
          LOG.debug("An error ocurred while resetting soTimeout, we will assume that no response is available.", localIOException1);
          break label158:
        }
      }
  }

  public boolean isSecure()
  {
    return this.protocolInUse.isSecure();
  }

  protected boolean isStale()
    throws IOException
  {
    int i = 1;
    if (this.isOpen)
      i = 0;
    try
    {
      int j = this.inputStream.available();
      if (j <= 0);
      try
      {
        this.socket.setSoTimeout(1);
        this.inputStream.mark(1);
        int k = this.inputStream.read();
        if (k == -1)
        {
          i = 1;
          return i;
        }
      }
      finally
      {
        this.socket.setSoTimeout(this.params.getSoTimeout());
      }
    }
    catch (InterruptedIOException localInterruptedIOException)
    {
      if (!(ExceptionUtil.isSocketTimeoutException(localInterruptedIOException)));
      throw localInterruptedIOException;
    }
    catch (IOException localIOException)
    {
      LOG.debug("An error occurred while reading from the socket, is appears to be stale", localIOException);
      i = 1;
    }
  }

  public boolean isStaleCheckingEnabled()
  {
    return this.params.isStaleCheckingEnabled();
  }

  public boolean isTransparent()
  {
    if ((!(isProxied())) || (this.tunnelEstablished));
    for (int i = 1; ; i = 0)
      return i;
  }

  public void open()
    throws IOException
  {
    LOG.trace("enter HttpConnection.open()");
    String str;
    label23: int i;
    if (this.proxyHostName == null)
    {
      str = this.hostName;
      if (this.proxyHostName != null)
        break label379;
      i = this.portNumber;
      label35: assertNotOpen();
      if (LOG.isDebugEnabled())
        LOG.debug("Open connection to " + str + ":" + i);
    }
    while (true)
    {
      try
      {
        int i3;
        if (this.socket == null)
        {
          if ((!(isSecure())) || (isProxied()))
            break label387;
          i3 = 1;
          label112: this.usingSecureSocket = i3;
          if ((!(isSecure())) || (!(isProxied())))
            break label393;
        }
        label379: ProtocolSocketFactory localProtocolSocketFactory;
        for (Object localObject = Protocol.getProtocol("http").getSocketFactory(); ; localObject = localProtocolSocketFactory)
        {
          this.socket = ((ProtocolSocketFactory)localObject).createSocket(str, i, this.localAddress, 0, this.params);
          this.socket.setTcpNoDelay(this.params.getTcpNoDelay());
          this.socket.setSoTimeout(this.params.getSoTimeout());
          int j = this.params.getLinger();
          if (j >= 0)
          {
            Socket localSocket = this.socket;
            if (j <= 0)
              break;
            bool = true;
            localSocket.setSoLinger(bool, j);
          }
          int k = this.params.getSendBufferSize();
          if (k >= 0)
            this.socket.setSendBufferSize(k);
          int l = this.params.getReceiveBufferSize();
          if (l >= 0)
            this.socket.setReceiveBufferSize(l);
          i1 = this.socket.getSendBufferSize();
          if (i1 > 2048)
            break label422;
          if (i1 <= 0)
            break label422;
          i2 = this.socket.getReceiveBufferSize();
          if (i2 > 2048)
            break label430;
          if (i2 <= 0)
            break label430;
          this.inputStream = new BufferedInputStream(this.socket.getInputStream(), i2);
          this.outputStream = new BufferedOutputStream(this.socket.getOutputStream(), i1);
          this.isOpen = true;
          return;
          str = this.proxyHostName;
          break label23:
          i = this.proxyPortNumber;
          break label35:
          label387: i3 = 0;
          break label112:
          label393: localProtocolSocketFactory = this.protocolInUse.getSocketFactory();
        }
        boolean bool = false;
      }
      catch (IOException localIOException)
      {
        closeSocketAndStreams();
        throw localIOException;
      }
      label422: int i1 = 2048;
      continue;
      label430: int i2 = 2048;
    }
  }

  public void print(String paramString)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.print(String)");
    write(EncodingUtil.getBytes(paramString, "ISO-8859-1"));
  }

  public void print(String paramString1, String paramString2)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.print(String)");
    write(EncodingUtil.getBytes(paramString1, paramString2));
  }

  public void printLine()
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.printLine()");
    writeLine();
  }

  public void printLine(String paramString)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.printLine(String)");
    writeLine(EncodingUtil.getBytes(paramString, "ISO-8859-1"));
  }

  public void printLine(String paramString1, String paramString2)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.printLine(String)");
    writeLine(EncodingUtil.getBytes(paramString1, paramString2));
  }

  public String readLine()
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.readLine()");
    assertOpen();
    return HttpParser.readLine(this.inputStream);
  }

  public String readLine(String paramString)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.readLine()");
    assertOpen();
    return HttpParser.readLine(this.inputStream, paramString);
  }

  public void releaseConnection()
  {
    LOG.trace("enter HttpConnection.releaseConnection()");
    if (this.locked)
      LOG.debug("Connection is locked.  Call to releaseConnection() ignored.");
    while (true)
    {
      return;
      if (this.httpConnectionManager != null)
      {
        LOG.debug("Releasing connection back to connection manager.");
        this.httpConnectionManager.releaseConnection(this);
      }
      LOG.warn("HttpConnectionManager is null.  Connection cannot be released.");
    }
  }

  public void setConnectionTimeout(int paramInt)
  {
    this.params.setConnectionTimeout(paramInt);
  }

  public void setHost(String paramString)
    throws IllegalStateException
  {
    if (paramString == null)
      throw new IllegalArgumentException("host parameter is null");
    assertNotOpen();
    this.hostName = paramString;
  }

  public void setHttpConnectionManager(HttpConnectionManager paramHttpConnectionManager)
  {
    this.httpConnectionManager = paramHttpConnectionManager;
  }

  public void setLastResponseInputStream(InputStream paramInputStream)
  {
    this.lastResponseInputStream = paramInputStream;
  }

  public void setLocalAddress(InetAddress paramInetAddress)
  {
    assertNotOpen();
    this.localAddress = paramInetAddress;
  }

  protected void setLocked(boolean paramBoolean)
  {
    this.locked = paramBoolean;
  }

  public void setParams(HttpConnectionParams paramHttpConnectionParams)
  {
    if (paramHttpConnectionParams == null)
      throw new IllegalArgumentException("Parameters may not be null");
    this.params = paramHttpConnectionParams;
  }

  public void setPort(int paramInt)
    throws IllegalStateException
  {
    assertNotOpen();
    this.portNumber = paramInt;
  }

  public void setProtocol(Protocol paramProtocol)
  {
    assertNotOpen();
    if (paramProtocol == null)
      throw new IllegalArgumentException("protocol is null");
    this.protocolInUse = paramProtocol;
  }

  public void setProxyHost(String paramString)
    throws IllegalStateException
  {
    assertNotOpen();
    this.proxyHostName = paramString;
  }

  public void setProxyPort(int paramInt)
    throws IllegalStateException
  {
    assertNotOpen();
    this.proxyPortNumber = paramInt;
  }

  public void setSendBufferSize(int paramInt)
    throws SocketException
  {
    this.params.setSendBufferSize(paramInt);
  }

  public void setSoTimeout(int paramInt)
    throws SocketException, IllegalStateException
  {
    this.params.setSoTimeout(paramInt);
    if (this.socket == null)
      return;
    this.socket.setSoTimeout(paramInt);
  }

  public void setSocketTimeout(int paramInt)
    throws SocketException, IllegalStateException
  {
    assertOpen();
    if (this.socket == null)
      return;
    this.socket.setSoTimeout(paramInt);
  }

  public void setStaleCheckingEnabled(boolean paramBoolean)
  {
    this.params.setStaleCheckingEnabled(paramBoolean);
  }

  public void setVirtualHost(String paramString)
    throws IllegalStateException
  {
    assertNotOpen();
  }

  public void shutdownOutput()
  {
    LOG.trace("enter HttpConnection.shutdownOutput()");
    try
    {
      Class[] arrayOfClass = new Class[0];
      Method localMethod = this.socket.getClass().getMethod("shutdownOutput", arrayOfClass);
      Object[] arrayOfObject = new Object[0];
      localMethod.invoke(this.socket, arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      LOG.debug("Unexpected Exception caught", localException);
    }
  }

  public void tunnelCreated()
    throws IllegalStateException, IOException
  {
    LOG.trace("enter HttpConnection.tunnelCreated()");
    if ((!(isSecure())) || (!(isProxied())))
      throw new IllegalStateException("Connection must be secure and proxied to use this feature");
    if (this.usingSecureSocket)
      throw new IllegalStateException("Already using a secure socket");
    if (LOG.isDebugEnabled())
      LOG.debug("Secure tunnel to " + this.hostName + ":" + this.portNumber);
    this.socket = ((SecureProtocolSocketFactory)this.protocolInUse.getSocketFactory()).createSocket(this.socket, this.hostName, this.portNumber, true);
    int i = this.params.getSendBufferSize();
    if (i >= 0)
      this.socket.setSendBufferSize(i);
    int j = this.params.getReceiveBufferSize();
    if (j >= 0)
      this.socket.setReceiveBufferSize(j);
    int k = this.socket.getSendBufferSize();
    if (k > 2048)
      k = 2048;
    int l = this.socket.getReceiveBufferSize();
    if (l > 2048)
      l = 2048;
    this.inputStream = new BufferedInputStream(this.socket.getInputStream(), l);
    this.outputStream = new BufferedOutputStream(this.socket.getOutputStream(), k);
    this.usingSecureSocket = true;
    this.tunnelEstablished = true;
  }

  public void write(byte[] paramArrayOfByte)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.write(byte[])");
    write(paramArrayOfByte, 0, paramArrayOfByte.length);
  }

  public void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.write(byte[], int, int)");
    if (paramInt1 < 0)
      throw new IllegalArgumentException("Array offset may not be negative");
    if (paramInt2 < 0)
      throw new IllegalArgumentException("Array length may not be negative");
    if (paramInt1 + paramInt2 > paramArrayOfByte.length)
      throw new IllegalArgumentException("Given offset and length exceed the array length");
    assertOpen();
    this.outputStream.write(paramArrayOfByte, paramInt1, paramInt2);
  }

  public void writeLine()
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.writeLine()");
    write(CRLF);
  }

  public void writeLine(byte[] paramArrayOfByte)
    throws IOException, IllegalStateException
  {
    LOG.trace("enter HttpConnection.writeLine(byte[])");
    write(paramArrayOfByte);
    writeLine();
  }
}

/* Location:           E:\Desktop\Android\wordstd\classes_dex2jar.jar
 * Qualified Name:     org.apache.commons.httpclient.HttpConnection
 * JD-Core Version:    0.5.3
 */